#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: backend.py 
@version:
@time: 2022/04/01 
"""
from flask import Flask, render_template
from flask_apscheduler import APScheduler
from flask_restful import Api, request
import os
import sys

sys.path.append(os.getcwd())
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False


@app.route("/")
def index():
    context = {
        'path': {
            'jenkins': request.url + 'jenkins',
            'k8s': request.url + 'k8s',
            'serve': request.url + 'serve',
            'coding': request.url + 'coding',
            'red': "https://tapdata.feishu.cn/sheets/shtcnknULBL4U4pE92lgrV1pAcb?sheet=1b1cd5",
            'db_info': "https://tapdata.feishu.cn/wiki/wikcnoOeMaMeh3MX9W5ojevDQlh?sheet=WznQvx&table=tbluEFvxdrpmFnoB&view=vewUvvDgw7"
        }
    }
    return render_template("index.html", **context)


@app.route("/jenkins")
def hello_jenkins():
    context = {'jenkins': {
        'dev': "http://jenkins.devops.tapdata.net/",
        'dev_1': 'http://jenkins.devops.tapdata.net/job/tapdaas-manual/',
        'dev_2': 'http://jenkins.devops.tapdata.net/view/%E4%BC%81%E4%B8%9A%E7%89%88/job/daas-v2/',
        'dev_dfs': 'http://jenkins.devops.tapdata.net/view/Cloud/',
        'test': "http://192.168.1.183:8080/",
        'test_1': "http://192.168.1.183:8080/job/Daas-dev-test/",
        'test_2': "http://192.168.1.183:8080/job/Daas2-dev-test/",
        'test_3': "http://192.168.1.183:8080/job/DFS-SIT/",
        'cloud': "http://36.134.131.197:8081/",
    }, 'index': request.url[:-7]}
    return render_template("01jenkins.html", **context)


@app.route("/k8s")
def hello_k8s():
    context = {'kubesphere': {
        'kube_dashboard': "http://devops.tapdata.net/dashboard",
        'kube_node': "http://devops.tapdata.net/clusters/host/nodes",
        'kube_workload': "http://devops.tapdata.net/clusters/host/deployments",
        'kube_services': "http://devops.tapdata.net/clusters/host/services",
        'v2': "http://devops.tapdata.net/clusters/host/projects/dev/deployments/sharecdc-tapdaas-tapdaas/resource-status"
    }, 'index': request.url[:-3]}
    return render_template("02kubesphere.html", **context)


@app.route("/serve")
def hello_test():
    context = {'test': {
        f'v1_{str(number)}': f'http://v1-{str(number)}.devops.tapdata.net/' for number in range(21, 51)
    }}
    for j in range(31, 48):
        context.get('test').pop(f'v1_{str(j)}')
    context.update(
        {
            'dfs': {
                'sit': 'http://sit.cloud.tapdata.net/console/#/workbench',
                '_test': 'http://test.cloud.tapdata.net/console/#/workbench',
                'cloud': 'https://cloud.tapdata.net/console/#/workbench',
            }
        }
    )
    context.update({'index': request.url[:-5]})
    context.update({'daas2': 'http://192.168.1.181:31787/#/dashboard'})
    context.update({'daas2test': 'http://192.168.1.181:30513/#/dashboard'})
    context.update({'daas2cloud': 'http://36.134.131.197:3039/#/dashboard'})
    return render_template("03test.html", **context)


@app.route("/coding")
def hello_coding():
    context = {
        'daas':
            {
                'sprint #52': 'https://tapdata.coding.net/p/tapdata/iterations/58873/issues',
                'sprint #51': 'https://tapdata.coding.net/p/tapdata/iterations/54559/issues',
                'sprint #50': 'https://tapdata.coding.net/p/tapdata/iterations/46725/issues',
            }
    }
    context.update({'index': request.url[:-6]})
    return render_template("04coding.html", **context)


class SchedulerConfig(object):
    interval = 30
    JOBS = [
        {'id': 'package_daas1',  # 任务id
         'func': '__main__:package_daas1',  # 任务执行程序
         'args': None,  # 执行程序参数
         'trigger': 'interval',  # 任务执行类型，定时器
         'seconds': interval,  # 任务执行时间，单位秒
         },
        {'id': 'package_daas2',  # 任务id
         'func': '__main__:package_daas2',  # 任务执行程序
         'args': None,  # 执行程序参数
         'trigger': 'interval',  # 任务执行类型，定时器
         'seconds': interval,  # 任务执行时间，单位秒
         },
        {'id': 'dfs',  # 任务id
         'func': '__main__:dfs',  # 任务执行程序
         'args': None,  # 执行程序参数
         'trigger': 'interval',  # 任务执行类型，定时器
         'seconds': interval,  # 任务执行时间，单位秒
         }
    ]


def package_daas1():
    from TapDevops.CI.cores.jenkins_package import JenkinsPackage

    JenkinsPackage('daas1').jenkins_package_time_task()


def package_daas2():
    from TapDevops.CI.cores.jenkins_package import JenkinsPackage

    JenkinsPackage('daas2').jenkins_package_time_task()


def dfs():
    from TapDevops.CI.cores.jenkins_package import JenkinsPackage

    JenkinsPackage('dfs').jenkins_package_time_task()


if __name__ == '__main__':
    app.config.from_object(SchedulerConfig)
    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()
    app.run(debug=False, host="0.0.0.0", port=5112)
